package com.wsc.metastock.model.bean;

import java.util.ArrayList;
import java.util.Arrays;

import com.wsc.metastock.constants.MessageConstants;
import com.wsc.metastock.exception.InvalidSplitCountException;
import com.wsc.metastock.util.DateUtil;

public class Config extends MessageBase implements MessageConstants {
    public String getDateFormat() {
        return messages.getMessage(DATE_FORMAT, EMPTY_ARGUMENTS, LOCALE);
    }

    public String getStartDate() {
        return messages.getMessage(START_DATE, EMPTY_ARGUMENTS, LOCALE);
    }

    public ArrayList<String> getAllSymbols() {
        ArrayList<String> list = new ArrayList<String>();
        String[] symbols = messages.getMessage(SYMBOLS, EMPTY_ARGUMENTS, LOCALE).split(",");
        list.addAll(Arrays.asList(symbols));
        return list;
    }

    public void checkSplit(ArrayList<Quote> quotes) throws InvalidSplitCountException {
        String[] symbols = messages.getMessage(SPLIT_SYMBOLS, EMPTY_ARGUMENTS, LOCALE).split(",");
        String[] dates = messages.getMessage(SPLIT_DATES, EMPTY_ARGUMENTS, LOCALE).split(",");
        String[] scales = messages.getMessage(SPLIT_SCALES, EMPTY_ARGUMENTS, LOCALE).split(",");
        if (symbols.length == dates.length && symbols.length == scales.length && scales.length == dates.length) {
            for (int i = 0; i < symbols.length; i++) {
                for (Quote quote : quotes) {
                    String symbol = symbols[i].substring(0, symbols[i].length() - 3);
                    if (symbol.equals(quote.getSymbol())) {
                        if (DateUtil.stringToDate(getDateFormat(), dates[i]).after(
                            DateUtil.stringToDate("yyyyMMdd", quote.getDate()))
                                || DateUtil.stringToDate(getDateFormat(), dates[i]).equals(
                                    DateUtil.stringToDate("yyyyMMdd", quote.getDate()))) {
                            quote.setHigh(new Double((new Double(quote.getHigh()).doubleValue())
                                    / (new Double(scales[i]).doubleValue())).toString());
                            quote.setLow(new Double((new Double(quote.getLow()).doubleValue())
                                    / (new Double(scales[i]).doubleValue())).toString());
                            quote.setOpen(new Double((new Double(quote.getOpen()).doubleValue())
                                    / (new Double(scales[i]).doubleValue())).toString());
                            quote.setClose(new Double((new Double(quote.getClose()).doubleValue())
                                    / (new Double(scales[i]).doubleValue())).toString());
                        }

                    }
                }

            }
        } else {
            throw new InvalidSplitCountException("Invalid count in the array defined in the config.properties.");
        }
    }
}
